package com.jgx.dao;
import com.jgx.bean.service503;
import com.jgx.util.Dbutil509;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class serviceDao503 {
    public List<service503> findAll() {
        Connection connection = Dbutil509.getConnection();
        if (connection == null) {
            System.out.println("数据库连接失败");
            return new ArrayList<>(); // 返回空列表，表示连接失败
        }
        List<service503> serviceList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from roomservice";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                service503 service = new service503();
                service.setId(resultSet.getInt(1));
                service.setRoomid(resultSet.getInt(2));
                service.setServicetTime(resultSet.getString(3));
                service.setYgName(resultSet.getString(4));


                serviceList.add(service);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Dbutil509.closeAll(resultSet, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for(service503 service : serviceList) {
            System.out.println(service.toString());
        }
        return serviceList;
    }

    public void deleteByid(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "delete from roomservice where ygID = ?";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Dbutil509.closeAll(null, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(service503 service)  {
        Connection connection = null;
        PreparedStatement statement =null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "INSERT INTO roomservice(RoomID,ServicetTime,ygName) VALUES(?,?,?)";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,service.getRoomid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2, service.getServicetTime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,service.getYgName());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void update(service503 service){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "UPDATE roomservice SET RoomID=?,ServicetTime=?,ygName=? where ygID=?" ;
        try {
            statement=connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(4,service.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,service.getRoomid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,service.getServicetTime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,service.getYgName());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

